package com.hibox.util;

import cn.hutool.core.util.PhoneUtil;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.hibox.common.exception.UtilException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Set;

/**
 * @author WangFK
 * @version 1.0
 * @since 2023/7/25 14:16
 */
@Configuration
@Data
@Slf4j
public class MessageSendUtil {
    /**
     * <dependency>
     * <groupId>com.cloopen</groupId>
     * <artifactId>java-sms-sdk</artifactId>
     * <version>1.0.4</version>
     * </dependency>
     * #云通讯 短信模板
     * server-Ip: app.cloopen.com
     * server-Port: 8883
     * account-SId: 2c94811c88bf350301898b61ad03328a
     * account-Token: 5ff28e7318204ee6aff847d7e6d4260e
     * #  //请使用管理控制台中已创建应用的APPID
     * app-Id: 2c94811c88bf350301898b86792332a3
     */
    @Value("${server-Ip}")
    private String serverIp;
    @Value("${server-Port}")
    private String serverPort;
    @Value("${account-SId}")
    private String accountSId;
    @Value("${account-Token}")
    private String accountToken;
    @Value("${app-Id}")
    private String appId;

    public boolean SendMsg(String phone, String code) {
        if (!PhoneUtil.isPhone(phone)) {
            throw new UtilException("手机号格式错误");
        }
        return SendMsg(phone, code, 5);
    }

    public boolean SendMsg(String phone, String code, Integer TimeOut) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        String templateId = "1";//使用的模板id
        String[] datas = {code, TimeOut.toString()};//格式:你的验证码是{code}，请于{2}分钟内正确输入
        HashMap<String, Object> result = sdk.sendTemplateSMS(phone, templateId, datas);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
        } else {
            //异常返回输出错误码和错误信息
            throw new UtilException("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        }
        return true;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public String getServerPort() {
        return serverPort;
    }

    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }

    public String getAccountSId() {
        return accountSId;
    }

    public void setAccountSId(String accountSId) {
        this.accountSId = accountSId;
    }

    public String getAccountToken() {
        return accountToken;
    }

    public void setAccountToken(String accountToken) {
        this.accountToken = accountToken;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }
}
